package com.bxm.springboot.test.controller;

import com.aliyun.openservices.shade.com.alibaba.fastjson.JSON;
import com.aliyun.openservices.shade.com.alibaba.fastjson.JSONObject;
import com.bxm.springboot.test.utils.OkHttpUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class WXLoginController {

    public static final String LOGIN_URL = "https://api.weixin.qq.com/sns/jscode2session";
    public static final String APP_ID =  "wx570f34ccd5e1e73f";
    public static final String APP_SECRET = "4c75cd7bd386345964a5eb22bdd830a1" ;

    @RequestMapping("/wxlogin")
    public JSONObject wxlogin(@RequestParam("code") String code){
        Map<String,Object> params = new HashMap<>();
        System.out.println(code);
//        https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code"
        params.put("appid",APP_ID);
        params.put("secret",APP_SECRET);
        params.put("js_code",code);
        params.put("grant_type","authorization_code");
        String response = null;
        try {
            response = OkHttpUtils.post(LOGIN_URL,params);
            System.out.println(response);
        } catch (IOException e) {
        }
        JSONObject obj = JSON.parseObject(response);
        return obj;
    }
}
